package com.oreilly.android.taskmanager;

import com.oreilly.android.taskmanager.adapter.TaskListAdapter;
import com.oreilly.android.taskmanager.tasks.Task;

import android.app.ListActivity;
import android.content.Intent;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;

import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ViewTaskActivity extends ListActivity {

	private static final int LOCATION_UPDATE_DISTANCE = 10;
	private static final int LOCATION_UPDATE_TIME = 1000;
	private static final int REQUEST_ADD_TASK = 0;
	private static final int REQUEST_EDIT_TASK = 1;
	private TaskManagerApplication app;
	private TaskListAdapter adapter;

	// private TextView locationText;

	// private LocationManager locationManager;
	// private Location latestLocation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_tasks);
		app = (TaskManagerApplication) getApplication();

		adapter = new TaskListAdapter(app.getAllTasks(), this);
		setListAdapter(adapter);
		ListView listView = getListView();
		listView.setOnItemLongClickListener(new TaskListItemLongClickListener());
		listView.setOnItemClickListener(new TaskListItemClickListener());

		setUpViews();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflator = getMenuInflater();
		inflator.inflate(R.menu.view_task, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.add_task:
			addTask();
			break;
		case R.id.remove_completed:
			removeCompletedTasks();
			break;
		case R.id.show_map:
			startShowTasksMapActivity();
			break;
		case R.id.filter_tasks:
			// filterTasks();
			break;
		}

		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == REQUEST_ADD_TASK && resultCode == RESULT_OK) {
			adapter.notifyDataSetChanged();
		}

		if (requestCode == REQUEST_EDIT_TASK && resultCode == RESULT_OK) {
			adapter.notifyDataSetChanged();
		}
	}

	private void removeCompletedTasks() {
		Long[] ids = adapter.removeCompletedTasks();
		app.deleteTasks(ids);
	}

	private void startShowTasksMapActivity() {
		Intent intent = new Intent(this, ShowTasksMapActivity.class);
		startActivity(intent);
	}

	private void addTask() {
		Intent intent = new Intent(ViewTaskActivity.this, AddTaskActivity.class);
		startActivityForResult(intent, REQUEST_ADD_TASK);
	}

	private void setUpViews() {
		Button quickTaskButton = (Button) findViewById(R.id.quick_task_button);
		final EditText quickTaskText = (EditText) findViewById(R.id.quick_task_text);

		quickTaskButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				String text = quickTaskText.getText().toString();
				if (!text.equals("")) {
					Task t = new Task(text);
					app.addTask(t);
					adapter.notifyDataSetChanged();
				}
			}
		});
	}

	private class TaskListItemLongClickListener implements
			AdapterView.OnItemLongClickListener {

		public boolean onItemLongClick(AdapterView<?> parent, View view,
				int position, long id) {
			Intent intent = new Intent(ViewTaskActivity.this,
					AddTaskActivity.class);
			Task t = adapter.getItem(position);
			intent.putExtra("TASK_ID", t.getId());
			startActivityForResult(intent, REQUEST_EDIT_TASK);

			return true;
		}
	}

	private class TaskListItemClickListener implements
			AdapterView.OnItemClickListener {

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			adapter.toggleTaskCompleteAtPosition(position);
			app.saveTask(adapter.getItem(position));
		}
	}
}